package main

import (
    zmq "github.com/pebbe/zmq4"
    "github.com/vmihailenco/msgpack"
    "encoding/hex"
    "log"
)

type Master struct {
}

func NewMaster() *Master {
    return &Master{}
}

func (m Master) start() {
    var err error
    responder, _ := zmq.NewSocket(zmq.REP)
    defer responder.Close()
    responder.Bind("tcp://0.0.0.0:4505")

    for {
        // Wait for next request from client
        msg, _ := responder.Recv(0)
        log.Printf("RX packet\n%s", hex.Dump([]byte(msg)))

        var raw1 []byte = []byte(msg)
        var req interface{}
        err = msgpack.Unmarshal(raw1, &req)
        if err != nil {
            // TODO - answer error and restart
            continue
        }
        log.Printf("RX message\n%v", req)

        /* Step 2. Reply */
        var reply string = "bad load"
        var raw2 []byte
        raw2, err = msgpack.Marshal(&reply)
        responder.Send(reply, 0)
        log.Printf("TX packet\n%s", hex.Dump(raw2))
    }
}
